#-*- coding: utf-8 -*-

import os

import random

from google.appengine.ext import webapp, db
from google.appengine.ext.webapp import template
from google.appengine.api import users

from gaesessions import get_current_session

from snowlevel.models import Greeting, Station, User, Station

class MainPage(webapp.RequestHandler):
    def get(self):
        
        stations = Station.gql("")
        
        session = get_current_session()
        
        user = session.get('me')
        
        if user:
            template_values = {
                'message' : "<strong>Bienvenue:</strong> " + user.login,
                'stations': stations,
                }
        else:
            template_values = {
                'stations': stations,
                }

        path = os.path.join(os.path.dirname(__file__), '../templates/index.html')
        self.response.out.write(template.render(path, template_values))
        
class Connexion(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), '../templates/connexion.html')
        
        session = get_current_session()
        if session.get('message'):
            template_values = {'message' : session.pop('message')}
        else:
            template_values = {}
        self.response.out.write(template.render(path, template_values))
        
    def post(self):
        login = self.request.get('login')
        password = self.request.get('password')
        
        luser = User.gql("WHERE login = '" + login + "' and password = '" + password + "'")
        user = luser.get()
        #print user.login
        
        session = get_current_session()
        
        if user:
            session['me'] = user
            self.redirect("/")
        else:
            session['message'] = "Vous vous êtes trompé de login et/ou de password"
            self.redirect("/connexion")
        
        



class Init(webapp.RequestHandler):
    def get(self):
        
        
        self.init_stations(['Super Devolui', 'Les Arcs', 'Auron', 'Chamonix', 'Les Deux Alpes', 'Isola 2000'])
        self.init_users(['admin','Super Devolui', 'Les Arcs', 'Auron', 'Chamonix', 'Les Deux Alpes', 'Isola 2000'])
        print "L'initialisation a réussie"

    def init_stations(self, stations):
        for station in stations:
            s = Station()
            s.nom = station
            s.enneigement_haut = random.uniform(1, 10)
            s.enneigement_bas = random.uniform(1, 10)
            s.remonte_total = random.randint(10, 20)
            s.remonte_ouverte = random.randint(2, 10)
            s.put()
            
    def init_users(self, users):
        for user in users:
            u = User()
            u.login = user
            u.password = user
            q = db.GqlQuery("SELECT * FROM Station WHERE nom = '" + user + "'")
            u.station = q.get()
            u.put()
            
"""
class MainPage(webapp.RequestHandler):
    def get(self):
        greetings_query = Greeting.all().order('-date')
        greetings = greetings_query.fetch(10)

        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login'

        template_values = {
            'greetings': greetings,
            'url': url,
            'url_linktext': url_linktext,
            }

        path = os.path.join(os.path.dirname(__file__), '../templates/home.html')
        self.response.out.write(template.render(path, template_values))
"""

class Guestbook(webapp.RequestHandler):
    def post(self):
        greeting = Greeting()

        if users.get_current_user():
            greeting.author = users.get_current_user()

        greeting.content = self.request.get('content')
        greeting.put()
        self.redirect('/')